//
//  ViewController.m
//  10_HomeWork
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "CarsTableViewCell.h"
#import "HeaderView.h"
#import "UIView+Frame.h"
#import "ModelOfCar.h"
#define kTableCellReuseOfCar @"carReuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *carTableView;
@property (nonatomic, strong) NSMutableArray *arrOfCycle;
@property (nonatomic, strong) NSMutableArray *arrOfModel;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handelData];
    [self createTableView];
}
#pragma mark - 数据解析
- (void)handelData {
    // 解析plist文件
    NSString *path = [[NSBundle mainBundle]pathForResource:@"YiChe" ofType:@"plist"];
    // 解析出的数据放入字典中
    NSDictionary *dicOfData = [NSDictionary dictionaryWithContentsOfFile:path];
    // 取出所有的key值, 并进行升序处理.
    NSArray *allKeysArray = [[dicOfData allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSString *key = [allKeysArray objectAtIndex:0];
    NSDictionary *dic1 = [dicOfData objectForKey:key];
    NSArray *arrayOfNews = [dic1 objectForKey:@"list"];
    NSArray *arrOfCycle = [dic1 objectForKey:@"cycleImage"];
    
    self.arrOfModel = [NSMutableArray array];
    self.arrOfCycle = [NSMutableArray array];
    for (NSDictionary *diction in arrayOfNews) {
        ModelOfCar *model = [[ModelOfCar alloc] init];
        model.title = [diction objectForKey:@"title"];
        model.imageURL = [diction objectForKey:@"picCover"];
        [_arrOfModel addObject:model];
    }
    for (NSDictionary *dic in arrOfCycle) {
        NSString *stringURL = [dic objectForKey:@"picCover"];
        [_arrOfCycle addObject:stringURL];
    }
}
#pragma mark - 创建tableView
- (void)createTableView {
    self.carTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_carTableView];
    _carTableView.delegate = self;
    _carTableView.dataSource = self;
    [_carTableView registerClass:[CarsTableViewCell class] forCellReuseIdentifier:kTableCellReuseOfCar];
    _carTableView.rowHeight = 80;
    HeaderView *header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.dm_width, 300) array:_arrOfCycle];
    _carTableView.tableHeaderView = header;
}
#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrOfModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        CarsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableCellReuseOfCar];
    ModelOfCar *model = _arrOfModel[indexPath.row];
    cell.model = model;
    return cell;
}
@end
